package com.yiyuwanglu.basecore.service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import com.yiyuwanglu.basecore.common.Pager;
import com.yiyuwanglu.basecore.dao.MongoDBBaseDao;
import com.yiyuwanglu.basecore.service.IShopDataService;
import com.yiyuwanglu.basecore.vo.WapDataVo;

@Service("shopDataService")
public class ShopDataServiceImpl implements IShopDataService {

	@Autowired
	private MongoDBBaseDao mongoDBBase;
	
	
	
	
	/**
	 * 店铺管理首页统计
	 * 访问量
	 */
	@Override
	public List<WapDataVo> shopHomeView(WapDataVo wap,String type){
		List<WapDataVo> list = null;
		Query query = new Query();
		
		Criteria ca = new Criteria();
		
		ca.and("createDate").regex(wap.getMonthStart()); //时间查询
		
		ca.and("type").is(type);  //店铺管理首页访问量
		query.addCriteria(ca);
		
		TypedAggregation<WapDataVo> agg = Aggregation.newAggregation(
				WapDataVo.class
				, Aggregation.match(ca)
				,Aggregation.project("createDate")
				.andExpression("substr(createDate,0,10)").as("publishDate")
				.and("shopId").as("shopId")
				, Aggregation.group("publishDate","shopId")
					.max("publishDate").as("createDate")
					.max("shopId").as("shopId")
					.count().as("count")
				, Aggregation.sort(Direction.DESC, "createDate")
		);
		list = mongoDBBase.aggregate(agg, "wap", WapDataVo.class).getMappedResults();
		return list;
	}
	
	
	
	/**
	 * 根据类型查询 时间分组
	 */
	@Override
	public List<WapDataVo> shopHomeLabels(WapDataVo wap,String type){
		List<WapDataVo> list = null;
		Query query = new Query();
		
		Criteria ca = new Criteria();
		
		ca.and("type").regex(type);  //店铺管理标签
		ca.and("createDate").regex(wap.getMonthStart()); //时间查询
		
		
		query.addCriteria(ca);
		
		TypedAggregation<WapDataVo> agg = Aggregation.newAggregation(
				WapDataVo.class
				, Aggregation.match(ca)
				,Aggregation.project("createDate")
				.andExpression("substr(createDate,0,10)").as("publishDate")
				.and("type").as("type")
				, Aggregation.group("publishDate","type")
				.max("publishDate").as("createDate")
				.max("type").as("type")
				.count().as("count")
				, Aggregation.sort(Direction.DESC, "createDate")
		);
		list = mongoDBBase.aggregate(agg, "wap", WapDataVo.class).getMappedResults();
		return list;
	}
	
	
	
	/**
	 * 查询内部理财师按钮点击等统计
	 * 类型  电话
	 */
	@Override
	public List<WapDataVo> shopFaUser(WapDataVo wap,String types,String mobiles,Pager page){
		List<WapDataVo> list = null;
		Query query = new Query();
		
		Criteria ca = new Criteria();
		
		ca.and("type").in(types.split(","));  //标签
		if(StringUtils.isNotBlank(mobiles)){
			ca.and("shopId").in(mobiles.split(","));  //电话标签
		}
		if(StringUtils.isNotBlank(wap.getMonthStart())){
			ca.and("createDate").regex(wap.getMonthStart()); //时间查询模糊
		}
		
		query.addCriteria(ca);
		
		TypedAggregation<WapDataVo> agg = Aggregation.newAggregation(
				WapDataVo.class
				, Aggregation.match(ca)
				,Aggregation.project("createDate")
				.andExpression("substr(createDate,0,10)").as("publishDate")
				.and("type").as("type")
				.and("shopId").as("shopId")
				, Aggregation.group("publishDate","type","shopId")
				.max("publishDate").as("createDate")
				.max("type").as("type")
				.max("shopId").as("shopId")
				.count().as("count")
				, Aggregation.sort(Direction.DESC, "createDate")
		);
		list = mongoDBBase.aggregate(agg, "wap", WapDataVo.class).getMappedResults();
		return list;
	}
	
	
	
	
	/**
	 *  根据 多手机号
	 *	 多type 统计总数
	 */
	@Override
	public List<WapDataVo> getByMobileType(WapDataVo wap,String types,String mobiles,Pager page){
		List<WapDataVo> list = null;
		Query query = new Query();
		
		Criteria ca = new Criteria();
		if(StringUtils.isNotBlank(types)){
			ca.and("type").in(types.split(","));  //标签
		}
		if(StringUtils.isNotBlank(mobiles)){
			ca.and("shopId").in(mobiles.split(","));  //电话标签
		}
		//时间区间
		if(StringUtils.isNotBlank(wap.getMonthStart())&&StringUtils.isNotBlank(wap.getMonthEnd())){
			ca.and("createDate").lte(wap.getMonthEnd()).gte(wap.getMonthStart());
		}else if(StringUtils.isNotBlank(wap.getMonthStart())){
			ca.and("createDate").gte(wap.getMonthStart());
		}else if(StringUtils.isNotBlank(wap.getMonthEnd())){
			ca.and("createDate").lte(wap.getMonthEnd());
		}
		
		query.addCriteria(ca);
		
		TypedAggregation<WapDataVo> agg = Aggregation.newAggregation(
				WapDataVo.class
				, Aggregation.match(ca)
				, Aggregation.group("type","shopId")
				.max("type").as("type")
				.max("shopId").as("shopId")
				.max("createDate").as("createDate")
				.count().as("count")
				, Aggregation.sort(Direction.DESC, "createDate")
				, Aggregation.skip((page.getPageSize() * (page.getPageNo() - 1)))
				, Aggregation.limit(page.getPageSize())
		);
		list = mongoDBBase.aggregate(agg, "wap", WapDataVo.class).getMappedResults();
		return list;
	}
	
	
	/**
	 * 总数统计
	 */
	@Override
	public List<WapDataVo> getAll(WapDataVo wap,String types,Pager page){
		List<WapDataVo> list = null;
		Query query = new Query();
		Criteria ca = new Criteria();
		if(StringUtils.isNotBlank(types)){
			ca.and("type").in(types.split(","));  //标签
			
		}
		//时间区间
		if(StringUtils.isNotBlank(wap.getMonthStart())){
			
			ca.and("createDate").lte(wap.getMonthEnd()).gte(wap.getMonthStart());
		}else{
			ca.and("createDate").lte(wap.getMonthEnd());
		}
		
		
		query.addCriteria(ca);
		
		TypedAggregation<WapDataVo> agg = Aggregation.newAggregation(
				WapDataVo.class
				, Aggregation.match(ca)
				,Aggregation.project("createDate")
				.andExpression("substr(createDate,0,10)").as("publishDate")
				.and("type").as("type")
//				.and("host").as("host")
				, Aggregation.group("type","publishDate")
				.max("type").as("type")
//				.max("host").as("host")
				.max("publishDate").as("createDate")
				.count().as("count")
				, Aggregation.sort(Direction.DESC, "createDate")
//				, Aggregation.skip((page.getPageSize() * (page.getPageNo() - 1)))
//				, Aggregation.limit(page.getPageSize())
		);
		list = mongoDBBase.aggregate(agg, "wap", WapDataVo.class).getMappedResults();
		return list;
	}
	
	
	//访客数按 数组
	/***
	 * types  模糊查询
	 */
	@Override
	public List<WapDataVo>  shopViewList(WapDataVo wap,String mobiles,String types,Pager page){
		List<WapDataVo> list=null;
		Query query = new Query();
		
		Criteria ca = new Criteria();
		if(StringUtils.isNotBlank(mobiles)){
			ca.and("shopId").in(mobiles.split(","));
		}else{
			ca.and("shopId").ne(null);
		}
		
		if(StringUtils.isNotBlank(wap.getMonthStart())){
			ca.and("createDate").lte(wap.getMonthStart());	
		}
		
		ca.and("type").regex(types);
		if(types.contains("^11010\\d")){
			ca.and("wechatId").ne(null);
		}
		
		query.addCriteria(ca);
		
		TypedAggregation<WapDataVo> agg = Aggregation.newAggregation(
				WapDataVo.class
				,Aggregation.match(ca)
				,Aggregation.project("createDate")
				.andExpression("substr(createDate,0,10)").as("publishDate")
				.and("wechatId").as("wechatId")
				.and("shopId").as("shopId")
				.and("count").as("count")
				, Aggregation.group("wechatId","shopId")
					.max("wechatId").as("wechatId")
					.max("shopId").as("shopId")
					.max("publishDate").as("createDate")
					.count().as("count")
				, Aggregation.group("shopId")
					.max("wechatId").as("wechatId")
					.max("shopId").as("shopId")
					.max("createDate").as("createDate")
					.sum("count").as("count")
					.count().as("viewCount")
				, Aggregation.sort(Direction.DESC, "createDate")
				
					
		);
		
		list = mongoDBBase.aggregate(agg, "wap", WapDataVo.class).getMappedResults();
		
		return list;
	}
	
	@Override
	public List<WapDataVo>  shopIdList(WapDataVo wap,String mobiles,String types,Pager page){
		List<WapDataVo> list=null;
		Query query = new Query();
		
		Criteria ca = new Criteria();
		if(StringUtils.isNotBlank(mobiles)){
			ca.and("shopId").in(mobiles.split(","));
		}else{
			ca.and("shopId").ne(null);
		}
		
		ca.and("type").regex(types);
		
		query.addCriteria(ca);
		
		TypedAggregation<WapDataVo> agg = Aggregation.newAggregation(
				WapDataVo.class
				,Aggregation.match(ca)
				, Aggregation.group("shopId")
					.max("shopId").as("shopId")
					.count().as("count")
				, Aggregation.skip((page.getPageSize() * (page.getPageNo() - 1)))
				, Aggregation.limit(page.getPageSize())
					
		);
		list = mongoDBBase.aggregate(agg, "wap", WapDataVo.class).getMappedResults();
		return list;
	}
	
	@Override
	public int  shopIdListAll(WapDataVo wap,String mobiles,String types){
		List<WapDataVo> list=null;
		Query query = new Query();
		
		Criteria ca = new Criteria();
		if(StringUtils.isNotBlank(mobiles)){
			ca.and("shopId").in(mobiles.split(","));
		}else{
			ca.and("shopId").ne(null);
		}
		
		ca.and("type").regex(types);
		
		query.addCriteria(ca);
		
		TypedAggregation<WapDataVo> agg = Aggregation.newAggregation(
				WapDataVo.class
				,Aggregation.match(ca)
				, Aggregation.group("shopId")
					.max("shopId").as("shopId")
					.max("createDate").as("createDate")
					.count().as("count")
				, Aggregation.sort(Direction.DESC, "createDate")
		);
		list = mongoDBBase.aggregate(agg, "wap", WapDataVo.class).getMappedResults();
		return list.size();
	}
	
	//访客数按 数组总数
	@Override
	public int  shopViewListCount(WapDataVo wap,String mobiles,Pager page){
		List<WapDataVo> list=null;
		Query query = new Query();
		
		Criteria ca = new Criteria();
		if(StringUtils.isNotBlank(mobiles)){
			ca.and("shopId").in(mobiles.split(","));
		}else{
			ca.and("shopId").ne(null);
		}
		
		if(StringUtils.isNotBlank(wap.getMonthStart())){
			ca.and("createDate").lte(wap.getMonthStart());	
		}
		
		ca.and("type").regex("^11010\\d");
		ca.and("wechatId").ne(null);
		
		
		query.addCriteria(ca);
		
		TypedAggregation<WapDataVo> agg = Aggregation.newAggregation(
				WapDataVo.class
				,Aggregation.match(ca)
				,Aggregation.project("createDate")
				.andExpression("substr(createDate,0,10)").as("publishDate")
				.and("wechatId").as("wechatId")
				.and("shopId").as("shopId")
				.and("count").as("count")
				, Aggregation.group("wechatId","shopId")
					.max("wechatId").as("wechatId")
					.max("shopId").as("shopId")
					.max("publishDate").as("createDate")
					.count().as("count")
				, Aggregation.group("shopId")
					.max("wechatId").as("wechatId")
					.max("shopId").as("shopId")
					.max("createDate").as("createDate")
					.sum("count").as("count")
					.count().as("viewCount")
				, Aggregation.sort(Direction.DESC, "createDate")
					
		);
		
		list = mongoDBBase.aggregate(agg, "wap", WapDataVo.class).getMappedResults();
		
		return list.size();
	}
	
}
